'use strict';

// simple stub that could use a lot of work...
myApp.factory('RESTService',
    function ($http) {
        return {
            get:function (url, callback) {
                return $http({method:'GET', url:url}).
                    success(function (data, status, headers, config) {
                        callback(data);
                        //console.log(data.json);
                    }).
                    error(function (data, status, headers, config) {
                        console.log("failed to retrieve data");
                    });
            }
        };
    }
);


// simple auth service that can use a lot of work... 
myApp.factory('AuthService',
    function () {
        var currentUser = null;
        var authorized = false;

        // initial state says we haven't logged in or out yet...
        // this tells us we are in public browsing
        var initialState = true;

        return {
            initialState:function () {
                return initialState;
            },
            login:function (name, password) {
                currentUser = name;
                authorized = true;
                //console.log("Logged in as " + name);
                initialState = false;
            },
            logout:function () {
                currentUser = null;
                authorized = false;
            },
            isLoggedIn:function () {
                return authorized;
            },
            currentUser:function () {
                return currentUser;
            },
            authorized:function () {
                return authorized;
            }
        };
    }
);
    myApp.factory('LiebautServer', function($http){
        return {
            getAll : function(resource, params){
                return $http(
                    {
                        method: 'GET',
                        url: 'http://localhost:8080/LiebautServer/' + resource,
                        params: params,
                        headers : {
                            'Accept' : 'application/json',
                            'Content-Type': 'application/json'
                        },
                        cache : false,
                        withCredentials : true
                    }
                );
            },
            get : function(resource, id, params){
                return $http(
                    {
                        method: 'GET',
                        url: 'http://localhost:8080/LiebautServer/' + resource+'/'+id,
                        params: params,
                        headers : {
                            'Accept' : 'application/json'
                        },
                        cache : false,
                        withCredentials : true
                    }
                );
            },
            save : function(resource, data) {
                return $http(
                    {
                        method: 'POST',
                        url: 'http://localhost:8080/LiebautServer/' + resource,
                        data: data,
                        headers : {
                            'Accept' : 'application/json'
                        },
                        cache : false,
                        withCredentials : true
                    }
                );
            }
        }
    });
    myApp.factory('GenericRESTService', function($http) {
	return {
		getAll : function(controller, sort, parameters) {
			return $http({
				method : 'GET',
				url : 'http://localhost:8080/' + controller,
				params: parameters,
				// data: '..... data for post/put requests...',
				headers : {
					'Accept' : 'application/json'
				},
				cache : false,
				withCredentials : true
			});
		}
	}
});

    myApp.factory('AlertService', function($rootScope, $timeout) {
        var alertService = {};
        $rootScope.alerts = [];
        alertService.add = function (type, msg, timeout) {
            $rootScope.alerts.push({ type: type, msg: msg, close: function () {
                return alertService.closeAlert(this);
            } }); // timeout
            if (timeout) {
                $timeout(function () {
                    alertService.closeAlert(this);
                }, timeout);
            }
        };
        alertService.closeAlert = function (alert) {
            return this.closeAlertIdx($rootScope.alerts.indexOf(alert));
        };
        alertService.closeAlertIdx = function (index) {
            return $rootScope.alerts.splice(index, 1);
        };
        return alertService;
    });



